package cn.itsub.proxy.client.config;

import cn.itsub.proxy.common.Config;
import com.sun.org.apache.xpath.internal.operations.Bool;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: 夏增明
 * @Date: 2021/11/02/9:44
 * @Description:
 */
@Slf4j
public class ClientProperties {

    private static ClientProperties instance=null;
    private static Config config = null;

    @Getter private String clientKey;   //客户端凭据
    @Getter private boolean sslEnable;  //是否启用SSL加密连接
    @Getter private String sslJksPath;  //SSL证书路径
    @Getter private String sslKeyStorePassword;  //SSL证书路径
    @Getter private String serverHost;  //服务器地址或域名
    @Getter private int serverPort;  //服务器端口


    private static String regex1 = "^-(.+)=(.+)$";

    /**
     * 加载程序参数，会覆盖配置文件的内容
     * @param args
     */
    public void loadArgs(String[] args){
        if (args != null && args.length > 0) {
            for (String arg : args) {
                if (Pattern.matches(regex1, arg)) {
                    Pattern pattern = Pattern.compile(regex1);
                    Matcher m = pattern.matcher(arg);
                    if (m.find( )) {
                        String key = m.group(1);
                        String value = m.group(2);
                        if ("key".equals(key)){
                            clientKey = value;
                        }
                        if ("h".equals(key)){
                            serverHost = value;
                        }
                        if ("p".equals(key)){
                            serverPort = Integer.parseInt(value);
                        }
                        if ("ssl".equals(key)){
                            sslEnable = Boolean.parseBoolean(value);
                        }
                        /*if ("jks".equals(key)){
                            sslJksPath = value;
                        }*/
                    } else {
                        System.out.println("NO MATCH");
                    }
                }

            }
        }
    }


    public static ClientProperties getInstance() {
        if (config == null) {
            synchronized (Config.class) {
                if (config == null) {
                    config = Config.getInstance();
                }
            }
        }
        if (instance == null) {
            synchronized (ClientProperties.class) {
                if (instance == null) {
                    instance = new ClientProperties();
                    instance.clientKey = config.getStringValue("client.key");
                    instance.serverHost = config.getStringValue("server.host");
                    instance.serverPort = config.getIntValue("server.port");
                    instance.sslEnable = config.getBooleanValue("ssl.enable");
                    instance.sslJksPath = config.getStringValue("ssl.jksPath");
                    instance.sslKeyStorePassword = config.getStringValue("ssl.keyStorePassword");
                }
            }
        }

        return instance;
    }
}
